package com.woniu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.common.result.Result;
import com.woniu.common.result.ResultCode;
import com.woniu.user.entity.PostFormResult;
import com.woniu.user.entity.UserInfo;
import com.woniu.user.entity.UserRealAuthentication;

import com.woniu.user.service.IUserInfoService;
import com.woniu.user.service.IUserRealAuthenticationService;
import com.woniu.user.util.RealAuthenticationUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贾玉亮
 * @since 2023-01-02
 */
@RestController
@RequestMapping("/user/user-real-authentication")
public class UserRealAuthenticationController {
    @Resource
    private IUserRealAuthenticationService userRealAuthenticationServiceImpl;
    @Resource
    private IUserInfoService userInfoServiceImpl;
    @Resource
    private RealAuthenticationUtil realAuthenticationUtil;
    @PostMapping("/IdCardAuthentication")
    public Result IdCardAuthentication(@RequestBody UserRealAuthentication userRealAuthentication){
        System.out.println(userRealAuthentication);
        //判断各种数据是否为空
        if (StringUtils.isBlank(userRealAuthentication.getRealName()) || StringUtils.isBlank(userRealAuthentication.getIdcardNumber())){
            return Result.error(ResultCode.PARAM_IS_BLANK.getCode(),ResultCode.PARAM_IS_BLANK.getMessage());
        }
        //讲姓名和身份证号已Map方式储存
        Map<String, String> params = new HashMap<>();
        params.put("idcard", userRealAuthentication.getIdcardNumber());
        params.put("name", userRealAuthentication.getRealName());
        try {
            String s = realAuthenticationUtil.postForm(params);
            System.out.println(s);
            ObjectMapper mapper = new ObjectMapper();
            PostFormResult postFormResult = mapper.readValue(s, PostFormResult.class);
            //判断是否一致,一致的话导入数据库,不一致导入
            String res = postFormResult.getResult().getRes();
            if (res.equals("1")){

                System.out.println("一致导入数据");
                userRealAuthentication.setAuditStatus("2");
                UserInfo user = userInfoServiceImpl.getById(userRealAuthentication.getFkUserId());
                //判断是否已认证
                Result<String> result = this.InferAuthentication(user.getUserMobile());
                String data =result.getData();
                if (data.equals("0")){
                    userRealAuthenticationServiceImpl.save(userRealAuthentication);
                }else if (data.equals("2")){
                    UpdateWrapper updateWrapper = new UpdateWrapper();
                    updateWrapper.eq("fk_user_id",userRealAuthentication.getFkUserId());
                    updateWrapper.set("real_name",userRealAuthentication.getRealName());
                    updateWrapper.set("idcard_number",userRealAuthentication.getIdcardNumber());
                    updateWrapper.set("front_picture",userRealAuthentication.getFrontPicture());
                    updateWrapper.set("opposite_picture",userRealAuthentication.getOppositePicture());
                    updateWrapper.set("people_picture",userRealAuthentication.getPeoplePicture());
                    updateWrapper.set("audit_status","2");
                    updateWrapper.set("documents_type",userRealAuthentication.getDocumentsType());
                    updateWrapper.set("update_time",new Date());
                    userRealAuthenticationServiceImpl.update(updateWrapper);
                }
            }else if (res.equals("2")){
                System.out.println("不一致报身份证或姓名错误异常");
                return Result.error(ResultCode.ERROR.getCode(),"不一致报身份证或姓名错误");
            }else if (res.equals("3")){
                System.out.println("不支持该类身份证异常");
                return Result.error(ResultCode.ERROR.getCode(),"不支持该类身份证异常");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    @GetMapping("/InferAuthentication/{mobile}")
    public Result InferAuthentication(@PathVariable("mobile") String mobile){
        int flag = userRealAuthenticationServiceImpl.InferAuthentication(mobile);
        //0 未审核 1审核中 2审核失败 3审核成功
        return Result.ok(flag+"");
    }

    @GetMapping("/auditAuthentication")
    public Result auditAuthentication(String userId){
        //修改实名认证表状态
        UpdateWrapper<UserRealAuthentication> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("fk_user_id",userId);
        objectUpdateWrapper.set("audit_status","1");
        userRealAuthenticationServiceImpl.update(objectUpdateWrapper);
        //修改用户表认证状态
        UpdateWrapper<UserInfo> objectUpdateWrapper1 = new UpdateWrapper<>();
        objectUpdateWrapper1.eq("id",userId);
        objectUpdateWrapper1.set("is_authentication","1");
        objectUpdateWrapper1.set("available_loan_amount",new BigDecimal("10000"));
        userInfoServiceImpl.update(objectUpdateWrapper1);
        return Result.ok();
    }
}

